<?php
/**
*
* @package acp
* @version $Id: acp_database.php 9417 2009-03-30 13:52:58Z acydburn $
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

include($phpbb_root_path . 'includes/functions_databasepro.' . $phpEx);

/**
* @package acp
*/
class acp_databasepro
{
	var $u_action;

	function main($id, $mode)
	{
		global $cache, $db, $user, $auth, $template, $table_prefix;
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
		
		$archive_store = 'somewhere';

		$user->add_lang('acp/database');
		$user->add_lang('acp/databasepro');

		$this->tpl_name = 'acp_databasepro';
		$this->page_title = 'ACP_DATABASEPRO';

		$action	= request_var('action', '');
		$submit = (isset($_POST['submit'])) ? true : false;

		$template->assign_vars(array(
			'MODE'	=> $mode
		));

		switch ($mode) {
			case 'backup':

				$this->page_title = 'ACP_BACKUPPRO';
				switch ($action) {
					case 'initialize':
						dbpro_initialize($this);
					break;
					
					case 'process':
						$trunk	= request_var('trunk', '');
						if ($trunk == 'bysize') {
							dbpro_process_bysize($this);
						} else {
							dbpro_process_byvolumn($this);
						}
					break;

					default:
						include($phpbb_root_path . 'includes/functions_install.' . $phpEx);
						$tables = get_tables($db);
						asort($tables);
						$no_backup_tables = array($table_prefix . 'search_results', $table_prefix . 'search_wordlist', $table_prefix . 'search_wordmatch', $table_prefix . 'sessions', $table_prefix . 'sessions_keys');
						$tables = array_diff($tables, $no_backup_tables);
						
						foreach ($tables as $table_name) {
							$template->assign_block_vars('tables', array(
								'TABLE'	=> $table_name
							));
						}
						unset($tables);

						$template->assign_vars(array(
							'U_ACTION'	=> $this->u_action . '&amp;action=initialize'
						));
					break;
				}
			break;

			case 'restore':

				$this->page_title = 'ACP_RESTOREPRO';

				switch ($action) {
					case 'submit':

						$showreadme = request_var('readme', '');
						$delete = request_var('delete', '');
						$download = request_var('download', '');
						$dorestore = request_var('restore', '');
						$file = request_var('file', '');

						$folder_name = $phpbb_root_path . 'store/' . $file;
						if (!file_exists($folder_name) || !is_file($folder_name . '/readme.txt')) {
							trigger_error($user->lang['BACKUP_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
						}
						
						if ($showreadme) {
							$readme_str = read_file_raw($folder_name . '/readme.txt');
							trigger_error($readme_str . '<br /><br />' . adm_back_link($this->u_action));
						}

						if ($delete) {
							if (confirm_box(true)) {
								if (delete_folder($folder_name)) {
									add_log('admin', 'LOG_DB_DELETE');
									trigger_error($user->lang['BACKUP_DELETE'] . adm_back_link($this->u_action));
								} else {
									add_log('admin', 'LOG_DB_DELETE');
									trigger_error($user->lang['GENERAL_ERROR'] . adm_back_link($this->u_action), E_USER_WARNING);
								}
							} else {
								confirm_box(false, $user->lang['DELETE_SELECTED_BACKUP'], build_hidden_fields(array('delete' => $delete, 'file' => $file)));
							}
							$this->show_restore_list();
							break;
						}
						
						if ($download)	{
							$archive_root = $phpbb_root_path . $archive_store . '/';
							$archivename = $file . '.zip';
							gen_zip_archive($folder_name . '/' , $archive_root, $archivename);
							$u_download = '<a href="'. $archive_root . $archivename . '">' . $user->lang['DOWNLOAD_BACKUP'] . '</a>';
							$u_remove = '<a href="'. $this->u_action . '&amp;action=del_archive&amp;file=' . $archivename . '">' . $user->lang['DELETE_BACKUP'] . '</a>';
							trigger_error($user->lang['BACKUP_ARCHIVE_DOWNLOAD'] . '<br /><br />' . $u_download . '<br /><br />' . $user->lang['BACKUP_ARCHIVE_DOWNLOAD_EXPLAIN'] . ' ' . $u_remove . adm_back_link($this->u_action));
						}
						
						if($dorestore) {
							restore_from_file($this);
						}

					break;

					case 'del_archive':
						$file = request_var('file', '');
						$file_name = $phpbb_root_path . $archive_store . '/' . $file;
						if (!is_file($file_name)) {
							trigger_error($user->lang['BACKUP_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
						}

						if (confirm_box(true)) {
							unlink($file_name);
							add_log('admin', 'LOG_DB_DELETE');
							trigger_error($user->lang['BACKUP_DELETE'] . adm_back_link($this->u_action));
						} else {
							confirm_box(false, $user->lang['DELETE_SELECTED_BACKUP'], build_hidden_fields(array('file' => $file)));
						}
						$this->show_restore_list();
					break;

					default:
						$this->show_restore_list();
					break;
				}
			break;
		}
	}
	
	function show_restore_list() {
		global $phpbb_root_path, $template;

		$dir = $phpbb_root_path . 'store/';
		$dh = @opendir($dir);
		$backup_files = array();
		if ($dh) {
			while (($file = readdir($dh)) !== false) {
				if($file != '.' && $file != '..' && is_dir($dir . '/' . $file) && is_file($dir . '/' . $file . '/readme.txt')) {
					$backup_files[] = $file;
				}
			}
			closedir($dh);
		}
		
		if (!empty($backup_files)) {
			krsort($backup_files);

			foreach ($backup_files as $file) {
				$template->assign_block_vars('files', array(
					'NAME'		=> $file,
					'SUPPORTED'	=> true,
				));
			}
		}
		
		$template->assign_vars(array(
			'U_ACTION'	=> $this->u_action . '&amp;action=submit'
		));
	}
}



?>